/*
 * VCResource.cpp
 *
 *  Created on: 2013. 9. 11.
 *      Author: mega@gamevil.com
 */

#include "VCResource.h"
#include <algorithm>
#include "../NUtil/Pool.h"

namespace ProjectZ { namespace NResource {

VCResource::VCResource()
{

}

VCResource::~VCResource()
{
	std::for_each(clsFlyTree.begin(), clsFlyTree.end(), ProjectZ::NUtil::del_second_ptr<DATATREE::value_type>);
}

void
VCResource::Clear()
{
	std::for_each(clsFlyTree.begin(), clsFlyTree.end(), ProjectZ::NUtil::del_second_ptr<DATATREE::value_type>);
	clsFlyTree.clear();
}

bool VCResource::LoadResource()
{
	::Database::Command::Auto clsAuto(::Database::GETCOMMANDPOOL());
	clsAuto->SetQueryType(::Database::Command::SELECT);
	clsAuto->MakeQuery("SELECT * FROM resource_vc_list");

	if (false == ::Database::GETSESSION()->SyncExecute(1, clsAuto))
		return false;

	if( clsAuto->GetErrorIdx() != 0 )
		return false;

	if (0 == clsAuto->ResultRowCount())
		return false;

	Clear();

	while( clsAuto->Next() )
	{
		::Toolkit::Str<3> NATIONAL_CODE;
		Toolkit::Str<10> vcKindDesc;
		Toolkit::Str<20> vcRegDate;

		VCResource::Flyweight * pFly = new VCResource::Flyweight;

		clsAuto->GetRecord() >> NATIONAL_CODE >> pFly->_VC_IDX >> pFly->_VC_DIV_1 >> pFly->_VC_DIV_2 >> pFly->_VC_DIV_3
				>> pFly->_VC_DESC >> pFly->_VC_PRICE >> pFly->_VC_KIND >> vcKindDesc >> vcRegDate;

		if (0 != Config::Instance().GetResourceDatabase()._strNational.compare(NATIONAL_CODE.c_str()))
		{
			DEBUGLOG("%s:%d NATIONAL_CODE ERROR!!! Config National Code [%s] , Resource National Code [%s]",
					__FILE__, __LINE__,
					Config::Instance().GetResourceDatabase()._strNational.c_str(),
					NATIONAL_CODE.c_str());
			return false;
		}

		clsFlyTree[pFly->_VC_IDX] = pFly;
	}

	return true;
}

VCResource::Flyweight * VCResource::GetFly(int vcIndex)
{
	auto iter = clsFlyTree.find(vcIndex);

	if (iter == clsFlyTree.end())
		return NULL;

	return iter->second;
}

} /* NResource */ } /* ProjectZ */
